//文件系统模块

const fs = require("fs");

//整体读取文本文件内容
fs.readFile("04_sample.txt","utf-8",(err,data)=>{
    if(err){
        console.error(err);
    }else{
        console.log(data);
    }
});
//整体读取二进制文件内容
fs.readFile("04_nodejs.jpg",(err,data)=>{
    if(err){
        console.error(err);
    }else{
        console.log(data);
        console.log(data.length);
    }
});

//fs回调形式  并使用Buffer
fs.open("04_sample.txt","r",function(err,fd){
    if(err) throw err;
    console.log("打开文件。。。");
    let buffer = Buffer.alloc(1024);
    fs.read(fd,buffer,0,buffer.length,0,function(err,bytesRead,buffer){
        if(err) throw err;
        console.log('读取字节${bytesRead}');
        console.log(buffer.subarray(0,bytesRead).toString());
        fs.close(fd,err=>{
            if(err) throw err;
        });
    });
});